﻿using Cmp.Web.Common;
using Cmp.Web.IService;
using Cmp.Web.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar.Extensions;
using System.Data;

namespace Cmp.Web.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PatientController : ControllerBase
    {
        private readonly IPatientService patientService;
        private ExceptionAll? errMsg = null;
        public PatientController(IPatientService service)
        {
            patientService = service;
        }
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult GetPatientInfoByCard(int cardType, string cardNum)
        {
            var result = patientService.GetPatientInfoByCard((CardType)cardType, cardNum, out errMsg).Result;
            if (errMsg != null)
            {
                return new MJsonResult(errMsg.errMsgId.ObjToInt(), errMsg.errMsg, result).GetResult();
            }
            else
            {
                return new MJsonResult(0, "", result).GetResult();
            }

        }
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult PatientDocumentation(PatientInfo patient)
        {
            var result = patientService.PatientDocumentation(patient, out errMsg).Result;
            if (errMsg != null)
            {
                return new MJsonResult(errMsg.errMsgId.ObjToInt(), errMsg.errMsg, result).GetResult();
            }
            else
            {
                return new MJsonResult(0, "", result).GetResult();
            }

        }
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult GetPatientInfoById(string patientId)
        {
            var result = patientService.GetPatientInfoByPatientId(patientId, out errMsg).Result;
            if (errMsg != null)
            {
                return new MJsonResult(errMsg.errMsgId.ObjToInt(), errMsg.errMsg, result).GetResult();
            }
            else
            {
                return new MJsonResult(0, "", result).GetResult();
            }

        }

    }
}
